package com.ruix.app.util;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.util.Log;

import com.ruix.app.RuiApplication;
import com.ruix.app.constant.RuiXConstant;
import com.tencent.mm.opensdk.modelpay.PayReq;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * Created by hadexs on 2017/7/18.
 */

public class WxpayOrderUtil {

    private static String TAG = "WxpayOrderUtil";

    private static PayReq payReq;// 请求实体
    private static IWXAPI msgApi;// 微信API

    public static void pay2Seller(Context context, Uri uri) {
        if (uri == null) {
            showErrorMsg();
            return;
        }

        String partnerId = uri.getQueryParameter("partnerid");
        String packageValue = uri.getQueryParameter("package");
        String prepayId = uri.getQueryParameter("prepayid");
        String nonceStr = uri.getQueryParameter("noncestr");
        String timeStamp = uri.getQueryParameter("timestamp");
        String sign = uri.getQueryParameter("sign");

        Log.e(TAG, "partnerId=" + partnerId);
        Log.e(TAG, "packageValue=" + packageValue);
        Log.e(TAG, "prepayId=" + prepayId);
        Log.e(TAG, "nonceStr=" + nonceStr);
        Log.e(TAG, "timeStamp=" + timeStamp);
        Log.e(TAG, "sign=" + sign);

        if (TextUtils.isEmpty(partnerId)
                || TextUtils.isEmpty(packageValue)
                || TextUtils.isEmpty(prepayId)
                || TextUtils.isEmpty(nonceStr)
                || TextUtils.isEmpty(timeStamp)
                || TextUtils.isEmpty(sign)) {
            showErrorMsg();
            return;
        }

        payReq = new PayReq();
        payReq.appId = RuiXConstant.WX_APP_ID;// 绑定支付等APPID
        payReq.partnerId = partnerId;// 商户号
        payReq.packageValue = packageValue;//  扩展参数，暂定［"Sign=WXPay"］
        payReq.prepayId = prepayId;// 预生成订单ID
        payReq.nonceStr = nonceStr;// 随机MD5串
        payReq.timeStamp = timeStamp;// 时间戳
        payReq.sign = sign;// 签名

        msgApi = WXAPIFactory.createWXAPI(RuiApplication.getAppContext(), null);
        msgApi.registerApp(RuiXConstant.WX_APP_ID);
        msgApi.sendReq(payReq);// 发送支付请求

    }

    public static void paySeller(String appid,String partnerId,String prepayId,String packageValue,String nonceStr,String timeStamp,String sign) {
        if (TextUtils.isEmpty(partnerId)
                || TextUtils.isEmpty(packageValue)
                || TextUtils.isEmpty(prepayId)
                || TextUtils.isEmpty(nonceStr)
                || TextUtils.isEmpty(timeStamp)
                || TextUtils.isEmpty(sign)) {
            showErrorMsg();
            return;
        }

        Log.e(TAG, "partnerId=" + partnerId);
        Log.e(TAG, "packageValue=" + packageValue);
        Log.e(TAG, "prepayId=" + prepayId);
        Log.e(TAG, "nonceStr=" + nonceStr);
        Log.e(TAG, "timeStamp=" + timeStamp);
        Log.e(TAG, "sign=" + sign);

        payReq = new PayReq();
        payReq.appId = appid;// 绑定支付等APPID
        payReq.partnerId = partnerId;// 商户号
        payReq.prepayId = prepayId;// 预生成订单ID
        payReq.packageValue = packageValue;//  扩展参数，暂定［"Sign=WXPay"］
        payReq.nonceStr = nonceStr;// 随机MD5串
        payReq.timeStamp = timeStamp;// 时间戳
        payReq.sign = sign;// 签名
        msgApi = WXAPIFactory.createWXAPI(RuiApplication.getAppContext(), null);
        msgApi.registerApp(appid);
        msgApi.sendReq(payReq);// 发送支付请求
    }


    public static boolean isInstalled(){
        msgApi = WXAPIFactory.createWXAPI(RuiApplication.getAppContext(), null);
        return msgApi.isWXAppInstalled();
    }

    // 显示错误信息
    private static void showErrorMsg() {
        ToastUtil.showNewMsg("支付参数不正确");
    }
}
